package com.xxwu.messagequeue.mqserver.datacenter;

import com.xxwu.messagequeue.common.MqException;
import com.xxwu.messagequeue.mqserver.core.Binding;
import com.xxwu.messagequeue.mqserver.core.Exchange;
import com.xxwu.messagequeue.mqserver.core.MSGQueue;
import com.xxwu.messagequeue.mqserver.core.Message;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.util.List;

/**
 * 实现统一硬盘管理
 */
@Slf4j
public class DiskDataCenter {
    DataBaseManager dataBaseManager = new DataBaseManager();
    MessageFileManager messageFileManager = new MessageFileManager();

    // 针对以上两个实例进行初始化
    public void init(){
        dataBaseManager.init();
        messageFileManager.init();
    }
    // 插入交换机
    public void insertExchange(Exchange exchange){

        dataBaseManager.insertIntoExchange(exchange);
        log.info("[DiskDataCenter] 插入交换机成功 exchangeName={}", exchange.getName());
    }
    // 查询所有交换机
    public List<Exchange> getAllExchanges(){
        return dataBaseManager.selectExchanges();
    }
    // 删除交换机
    public void deleteExchange(String exchangeName){
        dataBaseManager.deleteExchange(exchangeName);
        log.info("[DiskDataCenter] 删除交换机成功 exchangeName={}", exchangeName);
    }

    // 插入绑定
    public void insertBinding(Binding binding){
        dataBaseManager.insertIntoBinding(binding);
        log.info("[DiskDataCenter] 插入绑定成功 exchangeName={}, queueName={}"
                , binding.getExchangeName(), binding.getQueueName());
    }
    // 查询所有绑定
    public List<Binding> getAllBindings(){
        return dataBaseManager.selectBindings();
    }
    // 删除绑定
    public void deleteBinding(Binding binding){
        dataBaseManager.deleteBinding(binding);
        log.info("[DiskDataCenter] 删除绑定成功 exchangeName={}, queueName={}"
                , binding.getExchangeName(), binding.getQueueName());
    }

    // 插入队列
    public void insertMSGQueue(MSGQueue msgQueue){
        dataBaseManager.insertIntoMSGQueue(msgQueue);
        log.info("[DiskDataCenter] 插入队列成功 queueName={}", msgQueue.getName());
    }
    // 查询所有队列
    public List<MSGQueue> getAllMSGQueues(){
        return dataBaseManager.selectMSGQueues();
    }
    // 删除队列
    public void deleteMSGQueue(String queueName){
        dataBaseManager.deleteMSGQueue(queueName);
        log.info("[DiskDataCenter] 删除队列成功 queueName={}", queueName);
    }

    // 添加消息
    public void addMessage(MSGQueue queue, Message message) throws IOException, MqException {
        // 在添加消息之前要先创建数据文件才能插入
        messageFileManager.createQueueFile(queue.getName());
        messageFileManager.insertMessage(queue, message);
        log.info("[DiskDataCenter] 添加消息成功 messageId={}", message.getMessageId());
    }
    // 查询所有消息
    public List<Message> getAllMessages(String queueName) throws IOException, MqException, ClassNotFoundException {
        return messageFileManager.loadAllMessageFromQueue(queueName);
    }
    // 删除消息，在删除时要判断是否触发GC
    public void deleteMessage(MSGQueue queue, Message message) throws IOException, MqException, ClassNotFoundException {
        messageFileManager.deleteMessage(queue, message);
        log.info("[DiskDataCenter] 删除消息成功 messageId={}", message.getMessageId());
        if(messageFileManager.checkGC(queue.getName())){
            messageFileManager.gc(queue);
        }
    }
}
